package com.business.business.web.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.business.business.common.base.BaseService;
import com.business.business.model.BusinessBasisinfoFiles;
import com.business.business.model.BusinessRulesandregulations;
import com.business.business.support.model.RespEntity;
import com.business.business.support.mybatisplus.QueryWrapper;
import com.business.business.support.utils.requestutil.HttpRequestUtil;
import com.business.business.web.mapper.BusinessBasisinfoFilesMapper;
import com.business.business.web.mapper.BusinessRulesandregulationsMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class RulesandregulationsService extends BaseService {
    private static final Logger logger = LoggerFactory.getLogger(RulesandregulationsService.class);

    @Resource
    private BusinessRulesandregulationsMapper businessRulesandregulationsMapper;
    @Resource
    private BusinessBasisinfoFilesMapper businessBasisinfoFilesMapper;

    public RespEntity rulesandregulationsAdd(JSONObject jsonParam) {

        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());


        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        String userId = token.getString("user_id");
        String userType = token.getString("user_type");
        String businessId = "";
        if ("1".equals(userType)){
            businessId = jsonParam.getStr("businessId");
        }else {
            businessId = homeId;
        }
        //获取前台的数据
        String remarks = jsonParam.getStr("remarks");
        String name = jsonParam.getStr("name");
        String type = jsonParam.getStr("type");
        String fileName = jsonParam.getStr("fileName");
        String filePath = jsonParam.getStr("filePath");
        String bu = jsonParam.getStr("filePath");
        //自动生成ID
        String uuid = UUID.randomUUID().toString();
        //自动生成maketime
        Date date=new Date();
//        String makingTime = DateUtil.now();
        String createyear = DateUtil.year(date)+"";
        //初始化BusinessRulesandregulations
        BusinessRulesandregulations businessRulesandregulations = new BusinessRulesandregulations();
        businessRulesandregulations.setId(uuid);
        businessRulesandregulations.setType(type);
        businessRulesandregulations.setName(name);
        businessRulesandregulations.setBusinessId(businessId);
        businessRulesandregulations.setMakingtime(date);
        businessRulesandregulations.setCreateyear(createyear);
        boolean  businessRulesandregulation= businessRulesandregulationsMapper.insert(businessRulesandregulations)>0 ? true:false;
        BusinessBasisinfoFiles businessBasisinfoFiles = new BusinessBasisinfoFiles();
        businessBasisinfoFiles.setBusinessBasisinfoId(uuid);
        businessBasisinfoFiles.setFileName(fileName);
        businessBasisinfoFiles.setFilePath(filePath);
        boolean insertBusinessBasisinfoFiles = businessBasisinfoFilesMapper.insert(businessBasisinfoFiles) >0 ? true:false;
        if (businessRulesandregulation && insertBusinessBasisinfoFiles) {
            return RespEntity.SUCCESS;
        } else {
            return RespEntity.fail("插入失败");
        }

    }

    /**
     * @Author LiHaoDong
     * @Description 规章制度列表
     * @Date 16:22 2020/7/20
     * @Param [jsonParam]
     * @return com.business.business.support.model.RespEntity
     **/
    public RespEntity rulesandregulationsListPage(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        String userId = token.getString("user_id");
        String userType = token.getString("user_type");
        if ("2".equals(userType)){
            jsonParam.put("businessId",homeId);
        }
        Page<BusinessRulesandregulations> page = new Page<>(jsonParam.getInt("page"), jsonParam.getInt("limit"));
        IPage<BusinessRulesandregulations> iPage = this.businessRulesandregulationsMapper.rulesandregulationsListPage(page, jsonParam);
        List<BusinessRulesandregulations> records = iPage.getRecords();

        Map<String, Object> data = new HashMap<>();
        data.put("totalSize", iPage.getTotal());
        data.put("pages", iPage.getPages());
        data.put("pageSize", jsonParam.getInt("limit"));
        data.put("Items",records);
        data.put("currentPage",jsonParam.getInt("page"));
        data.put("currentSize",jsonParam.getInt("limit"));
        return RespEntity.success(data);
    }

    /**
     * @Author LiHaoDong
     * @Description 规章制度详情
     * @Date 17:24 2020/7/20
     * @Param [jsonParam]
     * @return com.business.business.support.model.RespEntity
     **/
    public RespEntity rulesandregulationsDetail(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");
        BusinessRulesandregulations businessRulesandregulations = this.businessRulesandregulationsMapper.rulesandregulationsDetail(jsonParam);
        List<BusinessBasisinfoFiles> basisinfoFilesList = businessBasisinfoFilesMapper.selectList(new QueryWrapper<BusinessBasisinfoFiles>().eq("business_basisinfo_id",jsonParam.getStr("id")));
        BusinessBasisinfoFiles basisinfoFiles= basisinfoFilesList.get(0);
        if (!businessRulesandregulations.isEmpty()) {
            businessRulesandregulations.put("fileInfo",basisinfoFiles);
            return RespEntity.success(businessRulesandregulations);
        }
        return RespEntity.fail("查询错误");
    }

    public RespEntity rulesandregulationsDelete(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");

        String id = jsonParam.getStr("id");
        int deleteBulesandregulations = this.businessRulesandregulationsMapper.deleteById(id);

        QueryWrapper queryWrapper = new QueryWrapper<BusinessBasisinfoFiles>();
        queryWrapper.eq("business_basisinfo_id", id);
        int deleteBusinessBasisinfoFiles = businessBasisinfoFilesMapper.delete(queryWrapper);

        if (deleteBulesandregulations > 0 && deleteBusinessBasisinfoFiles > 0) {
            return RespEntity.success();
        }
        return RespEntity.fail("删除失败");
    }

    /**
     * @Author LiHaoDong
     * @Description 规章制度修改
     * @Date 14:39 2020/7/22
     * @Param [jsonParam]
     * @return com.business.business.support.model.RespEntity
     **/
    public RespEntity rulesandregulationsUpdate(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());

        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String home_id = token.getString("home_id");

        String id = jsonParam.getStr("id");
        String remarks = jsonParam.getStr("remarks");
        String name = jsonParam.getStr("name");
        String type = jsonParam.getStr("type");
        String fileName = jsonParam.getStr("fileName");
        String filePath = jsonParam.getStr("filePath");

        //删除对应的附件表信息
        QueryWrapper queryWrapper = new QueryWrapper<BusinessBasisinfoFiles>();
        queryWrapper.eq("business_basisinfo_id", id);
        int deleteBusinessBasisinfoFiles = businessBasisinfoFilesMapper.delete(queryWrapper);
        //根据id将相关信息分别保存在规章制度表
        BusinessRulesandregulations businessRulesandregulations = new BusinessRulesandregulations();
        businessRulesandregulations.setId(id);
        businessRulesandregulations.setRemarks(remarks);
        businessRulesandregulations.setName(name);
        businessRulesandregulations.setType(type);
        int updateRulesandregulations = this.businessRulesandregulationsMapper.updateById(businessRulesandregulations);
        //business_basisinfo_files中
        BusinessBasisinfoFiles businessBasisinfoFiles = new BusinessBasisinfoFiles();
        businessBasisinfoFiles.setFileName(fileName);
        businessBasisinfoFiles.setFilePath(filePath);
        businessBasisinfoFiles.setBusinessBasisinfoId(id);
        int insertBasisinfoFiles = this.businessBasisinfoFilesMapper.insert(businessBasisinfoFiles);
        if (deleteBusinessBasisinfoFiles > 0 && updateRulesandregulations > 0 && insertBasisinfoFiles > 0) {
            return RespEntity.success();
        }
        return RespEntity.fail("更新失败");
    }
}
